# 管理员功能
import os
from functools import wraps
from .user_src import login_user_dict
from .user_src import get_username_pwd, creat_info, creat_dict, creat_path
from db import save_data
from conf import USER_INFO_DIR, USER_PWD_DIR


# 验证是否是管理员登录装饰器
def check_admin(func):
    @wraps(func)
    def inner(*args, **kwargs):
        '''验证是否是管理员'''
        if login_user_dict['role'] != 'admin':
            return "当前功能只有管理员才可以使用，请重新登录哟！"
        return func(*args, **kwargs)

    return inner


@check_admin
def add_user():
    username, password, role = get_username_pwd()
    if not username: return "添加失败！"
    if f"{username}_pwd.json" in os.listdir(USER_PWD_DIR):
        return f"该用户名{username}已被注册，请不要重复注册！"
    male, age, hobby = creat_info()
    if age == None:
        return ''
    user_dict = creat_dict(username=username, password=password, role=role)
    pwd_path = creat_path('pwd', username=username)
    pwd_msg = save_data(data=user_dict, path=pwd_path)
    if pwd_msg:
        return pwd_msg
    info_dict = creat_dict(username=username, male=male, age=age, hobby=hobby)
    info_path = creat_path('info', username=username)
    info_msg = save_data(data=info_dict, path=info_path)
    if info_msg:
        return info_msg
    return "写入员工信息成功！"


@check_admin
def del_one():
    del_user = input("请输入需要删除的员工信息:>>>").strip()
    if f"{del_user}_pwd.json" not in os.listdir(USER_PWD_DIR):
        return "当前所要删除的员工不存在！"
    else:
        confirm = input(f"请确认删除员工{del_user}(y/n):>>>").strip()
        if confirm.lower() != 'y':
            return ''
        os.remove(os.path.join(USER_PWD_DIR,f"{del_user}_pwd.json"))
        os.remove(os.path.join(USER_INFO_DIR,f"{del_user}_info.json"))
        return f"{del_user}员工信息删除成功！"


@check_admin
def del_all():
    confirm = input(f"请确认删除全部员工(y/n):>>>").strip()
    if confirm.lower() != 'y':
        return ''
    for path in os.listdir(USER_PWD_DIR):
        if path == "lea4ning_pwd.json":
            continue
        os.remove(os.path.join(USER_PWD_DIR,path))
    for path in os.listdir(USER_INFO_DIR):
        if path == "lea4ning_info.json":
            continue
        os.remove(os.path.join(USER_INFO_DIR,path))
    return "所有员工信息全部删除完毕！"


@check_admin
def renew_user():
    renew_user = input("请输入需要修改的员工姓名:>>>").strip()
    if f"{renew_user}_info.json" not in os.listdir(USER_INFO_DIR):
        return "当前所要修改的员工不存在！"
    username, password, role = get_username_pwd()
    if not username: return "更新失败！"
    male, age, hobby = creat_info()
    if age == None:
        return ''
    # 将信息保存到文件中
    info_dict = creat_dict(username=renew_user, male=male, age=age, hobby=hobby)
    info_path = creat_path(tag='info', username=renew_user)
    info_msg = save_data(info_dict, path=info_path)
    if info_msg:
        return info_msg
    # 将密码保存到文件中
    user_dict = creat_dict(role=role, username=renew_user, password=password)
    path = creat_path(tag='pwd', username=renew_user)
    msg = save_data(user_dict, path=path)
    if msg:
        return msg
    return "修改信息成功！"
